import { type ComponentConfig } from './DslType'
//元素DSl处理类
export class ElementDSL {
  private config: ComponentConfig
  constructor(component: any) {
    this.config = {
      component,
      props: {},
    }
  }
  //属性配置
  protected setProps(name: string, value: any) {
    this.config.props = this.config.props || {}
    this.config.props[name] = value
    return this
  }
  //样式配置
  protected setStyle(name: string, value: string) {
    this.config.style = this.config.style || {}
    this.config.style[name] = value
    return this
  }
  //插槽配置
  protected setSlot(name: string, value: string | ComponentConfig | ComponentConfig[]) {
    this.config.slots = this.config.slots || []
    this.config.slots.push({ name, content: value })
    return this
  }
  //事件配置
  protected setEvent(name: string, handler: (e: Event) => void) {
    this.config.events = this.config.events || {}
    this.config.events[name] = handler
    return this
  }
  //子组件
  protected setChildren(child: ElementDSL) {
    this.config.children = this.config.children || []
    this.config.children.push(child.config)
    return this
  }
  //构建:返回组件配置
  build(): ComponentConfig {
    return this.config
  }
  // 事件绑定
  on(eventName: string, handler: (...args: any[]) => void): this {
    return this.setEvent(eventName, handler)
  }

  // 子组件
  children(child: ElementDSL): this {
    return this.setChildren(child)
  }
}
